package com.liuceng.zen.service;

import com.liuceng.zen.mapper.DictRegionMapper;
import com.liuceng.zen.pojo.domain.DictRegion;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class DictRegionService extends BaseService<DictRegion> {
  @Autowired
  private DictRegionMapper dictRegionMapper;

  public List<DictRegion> findProvinces() {
    return dictRegionMapper.findProvinces();
  }

  public List<DictRegion> findCities(Integer provinceId) {
    return dictRegionMapper.findCities(provinceId);
  }

  public List<DictRegion> findArea(Integer cityId) {
    return dictRegionMapper.findAreas(cityId);
  }

  public List<DictRegion> findRegions(Integer level, Integer parentId) {
    List<DictRegion> regions = new ArrayList<>();
    switch (level) {
      case DictRegion.LEVEL_PROVINCE: {
        regions = findProvinces();
        break;
      }
      case DictRegion.LEVEL_CITY: {
        regions = findCities(parentId);
        break;
      }
      case DictRegion.LEVEL_AREA: {
        regions = findArea(parentId);
        break;
      }
    }
    return regions;
  }

  public String findListNameByIds(String[] area){
    Integer[] data = new Integer[area.length];
    for(int i=0;i<area.length;i++){
      data[i]=Integer.parseInt(area[i]);
    }
    List<String> names = dictRegionMapper.findListNameByIds(data);
    return StringUtils.join(names, ",");
  }
}
